package controller

import (
	"gitee.com/song-yao-shun/MQH/dto"
	"gitee.com/song-yao-shun/MQH/model"
	"gitee.com/song-yao-shun/MQH/response"
	"gitee.com/song-yao-shun/MQH/respository"
	"gitee.com/song-yao-shun/MQH/vo"
	"github.com/gin-gonic/gin"
	"strconv"
)

type IApplicationController interface {
	RestController
}

type ApplicationController struct {
	ApplicationRty respository.ApplicationRepository
}

func (a ApplicationController) Create(ctx *gin.Context) {
	// 接收参数
	var requestapplication vo.CreateApplicationRequest
	if err := ctx.ShouldBind(&requestapplication); err != nil {
		response.Fail(ctx, nil, "数据验证错误！")
		return
	}

	// 判断name是否已使用
	var total int64
	a.ApplicationRty.DB.Raw("SELECT count(*) FROM applications " +
		"WHERE name = ?",requestapplication.Name).Count(&total)
	if total != 0 {
		response.Fail(ctx, nil, "应用的名称已存在！")
		return
	}

	// 组装 新建信息
	app := model.Application{
		Name:	requestapplication.Name,
		Img: 	requestapplication.Img,
		Desc: 	requestapplication.Desc,
	}
	application, err := a.ApplicationRty.Create(app)
	if err != nil {
		response.Fail(ctx, nil, "新建失败！")
		return
	}

	// 返回
	response.Success(ctx, gin.H{"data":application}, "新建成功！")
}

func (a ApplicationController) Update(ctx *gin.Context) {
	// 接收参数
	var requestApplication vo.CreateApplicationRequest
	if err := ctx.ShouldBind(&requestApplication); err != nil {
		response.Fail(ctx, nil, "数据验证错误")
		return
	}

	// 获取 path 中的参数
	applicationId, _ := strconv.Atoi(ctx.Params.ByName("id"))
	updateApplication, err := a.ApplicationRty.SelectById(applicationId)
	if err != nil {
		response.Fail(ctx, nil, "应用不存在！")
		return
	}

	// 更新 struct
	updateApplication.Name 	= requestApplication.Name
	updateApplication.Img 	= requestApplication.Img
	updateApplication.Desc 	= requestApplication.Desc

	application, err := a.ApplicationRty.Update(*updateApplication)
	if err != nil {
		panic(err)
	}

	response.Success(ctx, gin.H{"data":application}, "修改成功。")
}

func (a ApplicationController) Show(ctx *gin.Context) {
	// 获取 path 中的参数
	applicationId, _ := strconv.Atoi(ctx.Params.ByName("id"))

	application, err := a.ApplicationRty.SelectById(applicationId)
	if err != nil {
		response.Fail(ctx, nil, "应用不存在！")
		return
	}

	response.Success(ctx, gin.H{"data":application}, "")
}

func (a ApplicationController) Delete(ctx *gin.Context) {

	// 获取 path 中的参数
	applicationId, _ := strconv.Atoi(ctx.Params.ByName("id"))

	// 判断删除的id是否为部分page的外键
	var total int64
	p := respository.NewPageRepository()
	p.DB.Model(&model.Page{}).Where("application_id = ?",applicationId).Count(&total)
	if total != 0 {
		response.Fail(ctx, nil, "删除失败，该 应用下 存在页面")
		return
	}

	if rowsAffected := a.ApplicationRty.DeleteById(applicationId);rowsAffected != 1 {
		response.Fail(ctx, nil, "删除失败，请重试！")
		return
	}

	response.Success(ctx, nil, "删除成功！")
}

func (a ApplicationController) List(ctx *gin.Context) {
	// 接收参数
	var appList vo.ApplicationList
	if err := ctx.ShouldBind(&appList); err != nil {
		response.Fail(ctx, nil, "数据验证错误，请检查提交的内容！")
		return
	}

	apps, total := a.ApplicationRty.List(appList)

	if appList.SelectType != ""{
		response.Success(ctx, gin.H{"data":dto.ToAppDto(apps), "total":total}, "查询列表成功。")
		return
	}
	response.Success(ctx, gin.H{"data":apps, "total":total}, "查询列表成功。")
}

func  NewApplicationController() IApplicationController{
	return ApplicationController{ApplicationRty: respository.NewApplicationRepository()}
}